package com.lindroy.jetpacknotes.paging.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.lindroy.jetpacknotes.R
import com.lindroy.jetpacknotes.logic.model.ArticleResp

/**
 * @author Lin
 * @date 2022/1/14
 * @function
 */
class ArticleAdapter : PagingDataAdapter<ArticleResp, ArticleAdapter.ViewHolder>(COMPARATOR) {

    companion object {
        private val COMPARATOR = object : DiffUtil.ItemCallback<ArticleResp>() {
            override fun areItemsTheSame(oldItem: ArticleResp, newItem: ArticleResp) =
                oldItem.id == newItem.id

            override fun areContentsTheSame(oldItem: ArticleResp, newItem: ArticleResp) =
                oldItem == newItem
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvPosition = itemView.findViewById<TextView>(R.id.tvPosition)
        val tvTitle = itemView.findViewById<TextView>(R.id.tvTitle)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tvPosition.text = position.toString()
        holder.tvTitle.text = getItem(position)?.title
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.item_article_list, parent, false)
    )
}